package com.tlx.core.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

public class TFileUtils {

    public static final List<String> IMG_EXT;

    static {
        String imgExtStr = TConfig.getString("img.ext");
        String extArray[] = imgExtStr.split(",");
        IMG_EXT = Arrays.asList(extArray);
    }

    public static void rename(String oldPath, String newPath) {
        File oldFile = new File(oldPath);
        File newFile = new File(newPath);
        if (oldFile.equals(newFile)) {
            return;
        }
        String realRename = System.getProperty("realRename");
        if (realRename != null && Boolean.valueOf(realRename)) {
            boolean b = oldFile.renameTo(newFile);
            if (!b) {
                PrintUtils.print("重命名失败【" + oldFile.getAbsolutePath() + "】【" + newFile.getAbsolutePath() + "】");
            }
        } else {
            PrintUtils.print("即将重命名【" + oldFile.getAbsolutePath() + "】\t\t【" + newFile.getAbsolutePath() + "】");
        }
    }

    // 只修改文件名，不修改目录
    public static void renameOnlyName(String oldPath, String newName) {
        File oldFile = new File(oldPath);
        String newPath = new File(oldFile.getParent(), newName).getAbsolutePath();
        TFileUtils.rename(oldPath, newPath);
    }

    public static void deleteQuietly(String filePath) {
        File f = new File(filePath);
        if (!f.exists()) {
            return;
        }
        boolean isSuccess = FileUtils.deleteQuietly(f);
        if (!isSuccess) {
            PrintUtils.printErr("删除文件失败【" + filePath + "】");
        }
    }

    public static void deleteListQuietly(List<String> filePathList) {
        if (CollectionUtils.isNotEmpty(filePathList)) {
            for (String string : filePathList) {
                deleteQuietly(string);
            }
        }
    }

    public static String formatFileSize(long size) {
        long kb = 1024;
        long mb = kb * 1024;
        long gb = mb * 1024;

        if (size >= gb) {
            return String.format("%.1f GB", (float) size / gb);
        } else if (size >= mb) {
            float f = (float) size / mb;
            return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
        } else if (size >= kb) {
            float f = (float) size / kb;
            return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
        } else
            return String.format("%d B", size);
    }

    // 扩展名不传，获取全部
    public static List<String> getFileListByExt(String dirPath, String... extArray) {
        List<String> extList = Arrays.asList(extArray);
        List<String> result = new ArrayList<String>();

        File[] fileList = new File(dirPath).listFiles();
        if (fileList == null || fileList.length == 0) {
            return result;
        }
        for (File file : fileList) {
            String ext = FilenameUtils.getExtension(file.getName());
            if (CollectionUtils.isEmpty(extList)) {
                result.add(file.getAbsolutePath());
            } else if (extList.contains(ext.toLowerCase()) && file.isFile()) {
                result.add(file.getAbsolutePath());
            }
        }

        return result;
    }

    // 获取某个目录下全部目录
    public static List<String> getDirList(String dirPath) {
        List<String> result = new ArrayList<String>();

        File[] fileList = new File(dirPath).listFiles();

        if (fileList == null || fileList.length == 0) {
            return result;
        }

        for (File file : fileList) {
            if (file.isDirectory()) {
                result.add(file.getAbsolutePath());
            }
        }

        return result;
    }

    // 获取某个目录下全部文件
    public static List<String> getFileList(String dirPath) {
        List<String> result = new ArrayList<String>();

        File[] fileList = new File(dirPath).listFiles();

        if (fileList == null || fileList.length == 0) {
            return result;
        }

        for (File file : fileList) {
            if (file.isFile()) {
                result.add(file.getAbsolutePath());
            }
        }

        return result;
    }

    // 获取某个目录下全部图片
    public static List<String> getImageList(String dir) {
        List<String> fileList = getFileListByExt(dir, (String[]) IMG_EXT.toArray());

        Collections.sort(fileList);

        return new ArrayList<String>(fileList);
    }

    public static boolean isImage(String fileName) {
        String ext = FilenameUtils.getExtension(fileName);
        return IMG_EXT.contains(ext.toLowerCase());
    }

    public static boolean hasImage(String dirPath) {
        return CollectionUtils.isNotEmpty(getImageList(dirPath));
    }

    public static void copy(String srcPath, String destPath) {
        try {
            FileUtils.copyFile(new File(srcPath), new File(destPath));
        } catch (Exception e) {
            System.out.println(destPath);
            throw new RuntimeException(e);
        }
    }

}
